package com.cwz.datasource.dynamic.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.extension.MybatisMapWrapperFactory;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.mapping.DatabaseIdProvider;
import org.apache.ibatis.mapping.VendorDatabaseIdProvider;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Properties;

/**
 * @program: w-demo
 * @description: mybatis-plus 配置
 * @author: Wen
 **/
@Configuration
@MapperScan("com.cwz.**.mapper")
public class MybatisPlusConfig {
    @Autowired
    private DynamicRoutingDataSource dynamicRoutingDataSource;
    @Autowired
    private MyBatisPlusMetaObjectHandler myBatisPlusMetaObjectHandler;

    @Bean
    public MybatisSqlSessionFactoryBean sqlSessionFactoryBean() throws IOException {
        MybatisSqlSessionFactoryBean sessionFactory = new MybatisSqlSessionFactoryBean();
        // 注册分页插件
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        sessionFactory.setPlugins(interceptor);
        // mybatis plus 查询 map 时可返回驼峰命名
        sessionFactory.setObjectWrapperFactory(new MybatisMapWrapperFactory());
        sessionFactory.setDataSource(dynamicRoutingDataSource);
        sessionFactory.setDatabaseIdProvider(databaseIdProvider());
        // 配置文件
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        sessionFactory.setTypeAliasesPackage("com.cwz.**.entity");
        sessionFactory.setMapperLocations(resolver.getResources("classpath*:mapper/*.xml"));
        MybatisConfiguration configuration = new MybatisConfiguration();
        configuration.setMapUnderscoreToCamelCase(true);
        sessionFactory.setConfiguration(configuration);
        // global config
        GlobalConfig globalConfig = new GlobalConfig();
        globalConfig.setMetaObjectHandler(myBatisPlusMetaObjectHandler);
        sessionFactory.setGlobalConfig(globalConfig);
        return sessionFactory;
    }


    /**
     * @Description: Mybatis多数据库支持适配器
     * @Author: Wen
     * @return: org.apache.ibatis.mapping.DatabaseIdProvider
     **/
    @Bean
    public DatabaseIdProvider databaseIdProvider() {
        DatabaseIdProvider databaseIdProvider = new VendorDatabaseIdProvider();
        Properties p = new Properties();
        p.setProperty("Oracle", "oracle");
        p.setProperty("MySQL", "mysql");
        p.setProperty("PostgreSQL", "postgresql");
        p.setProperty("DB2", "db2");
        p.setProperty("SQL Server", "sqlserver");
        databaseIdProvider.setProperties(p);
        return databaseIdProvider;
    }

    /**
     * @Description: 事务管理器
     * @Author: Wen
     * @return: org.springframework.transaction.PlatformTransactionManager
     **/
    @Bean
    public DataSourceTransactionManager transactionManager() throws SQLException {
        // 配置事务管理, 使用事务时在方法头部添加 @Transactional 注解即可
        return new DataSourceTransactionManager(dynamicRoutingDataSource);
    }

    @Bean
    public JdbcTemplate jdbcTemplate() throws SQLException {
        return new JdbcTemplate(dynamicRoutingDataSource);
    }
}
